package com.molyfun.parents.activity.common.listactivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.molyfun.parents.activity.common.listactivity.factory.ListActivityFactory;
import com.molyfun.parents.fragment.dialog.CommonDeleteDialogFragment;
import com.molyfun.parents.utils.Const;

import static com.molyfun.parents.activity.classroom.ClassRoomActivity.FLAG_CLASS_IS_TEAM;
import static com.molyfun.parents.activity.common.listactivity.factory.ListActivityFactory.FLAG_CLASS_ROOM_PLAN_ID;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/28/16.
 */
public class ListActivity extends BaseListActivity implements AbstractListFactory.OnListOperationCallback{

    public static final String ACTIVITY_TYPE = "activity_type";

    private static final String TRAINING_ID = "trainingid";

    private static final String CUSTOMPLAN_ID = "customplanid";

    private static final String ANNOUNCEMENT_ID = "announcement_id";

    public static void startActivity(Context context, int activityType){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(ACTIVITY_TYPE, activityType);
        context.startActivity(intent);
    }

    public static void startAnnouncementActivity(Context context, String announcementId){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(ANNOUNCEMENT_ID, announcementId);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String trainingid){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(TRAINING_ID, trainingid);
        context.startActivity(intent);
    }

    public static void startCustomPlanProductActivity(Context context, String customplanid){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(CUSTOMPLAN_ID, customplanid);
        context.startActivity(intent);
    }

    /**
     * 话题评论列表使用该入口
     * @param context
     * @param topicId 话题id
     */
    public static void startActivity(Context context, String topicId, String userId, boolean isAnonymous){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(Const.TOPIC_ID, topicId);
        intent.putExtra(Const.USER_ID, userId);
        intent.putExtra(Const.IS_ANONYMOUS, isAnonymous);
        context.startActivity(intent);
    }

    public static void startClassRoomPlanActivity(Context context, String classRoomId, boolean isTeam){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(FLAG_CLASS_ROOM_PLAN_ID, classRoomId);
        intent.putExtra(ACTIVITY_TYPE, ListActivityFactory.ActivityType.ACTIVITY_CLASSROOMPLAN);
        intent.putExtra(FLAG_CLASS_IS_TEAM, isTeam);
        context.startActivity(intent);
    }

    public static void startClassRoomVideoRecordActivity(Context context, String classRoomId){
        Intent intent = new Intent();
        intent.setClass(context, ListActivity.class);
        intent.putExtra(FLAG_CLASS_ROOM_PLAN_ID, classRoomId);
        intent.putExtra(ACTIVITY_TYPE, ListActivityFactory.ActivityType.ACTIVITY_CLASSROOM_VIDEO_RECORD);
        context.startActivity(intent);
    }


//    public static void startClassRoomCourseListActivity(Context context, String classRoomId){
//        Intent intent = new Intent();
//        intent.setClass(context, ListActivity.class);
//        intent.putExtra(FLAG_CLASS_ROOM_PLAN_ID, classRoomId);
//        intent.putExtra(ACTIVITY_TYPE, ListActivityFactory.ActivityType.ACTIVITY_CLASSROOM_COURSE);
//        context.startActivity(intent);
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //init factory first
        initIntent();
        super.onCreate(savedInstanceState);
    }

    private void initIntent(){
        Intent intent = getIntent();
        String topic_id = intent.getStringExtra(Const.TOPIC_ID);
        String trainingid = intent.getStringExtra(TRAINING_ID);
        String customplanid = intent.getStringExtra(CUSTOMPLAN_ID);
        String announcementId = intent.getStringExtra(ANNOUNCEMENT_ID);
        if(!TextUtils.isEmpty(topic_id)){
            mListFactory = ListActivityFactory.getTopicCommentFactory(topic_id, getIntent().getStringExtra(Const.USER_ID), getIntent().getBooleanExtra(Const.IS_ANONYMOUS, false));
        }else if(!TextUtils.isEmpty(trainingid)) {
            mListFactory = ListActivityFactory.getReportFeedbackFactory(trainingid);
        }else if(!TextUtils.isEmpty(customplanid)){
            mListFactory = ListActivityFactory.getCustomPlanProduct(customplanid);
        }else if(!TextUtils.isEmpty(announcementId)){
            mListFactory = ListActivityFactory.getAnnouncementFactory(announcementId);
        }else{
            mListFactory = ListActivityFactory.getFactory(getIntent());
        }
        mListFactory.setOnListOperationCallback(this);

    }

    @Override
    public void onDataDelete(int position) {
        mAdapter.remove(position);
    }

    @Override
    public void onLongClick(final String id,final int position) {

        new CommonDeleteDialogFragment.CommonDeleteDialogBuilder("确定删除吗?")
                .setCommonDeleteDialogClickListener(new CommonDeleteDialogFragment.CommonDeleteDialogClickListener() {
                    @Override
                    public void onDeleteConfirm() {
                        mListFactory.deleteData(id, position);
                    }

                    @Override
                    public void onDeleteCancel() {

                    }
                })
                .build(getSupportFragmentManager().beginTransaction());

    }
}
